﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Spider
{
    public partial class frmRules : Form
    {
        public frmRules()
        {
            InitializeComponent();
        }

        #region Method
        private void blank()
        {
            this.txtBoxName.Text = string.Empty;
            this.txtBoxURLPrefix.Text = string.Empty;
            this.txtBoxTitleRegex.Text = string.Empty;
            this.txtBoxDescriptionRegex.Text = string.Empty;
            this.txtBoxImageURLRegex.Text = string.Empty;
            this.txtBoxPriceRegex.Text = string.Empty;
            this.txtProductUrlFilter.Text = string.Empty;
            this.txtCharset.Text = string.Empty;
        }

        private void addRule()
        {
            String urlPrefix = this.txtBoxURLPrefix.Text.Trim();
            int urlPrefixLenth = urlPrefix.Length;
            if (urlPrefix.Substring(urlPrefixLenth - 2).Equals("/"))
            {
                urlPrefix = urlPrefix.Substring(0, urlPrefixLenth - 1);
            }

            if (!urlPrefix.StartsWith("http://"))
                urlPrefix = "http://" + urlPrefix;

            Rules rule = new Rules();
            rule.Name = this.txtBoxName.Text;
            rule.UrlPrefix = urlPrefix;
            rule.TitleRegex = this.txtBoxTitleRegex.Text;
            rule.DescriptionRegex = this.txtBoxDescriptionRegex.Text;
            rule.ImageUrlRegex = this.txtBoxImageURLRegex.Text;
            rule.PriceRegex = this.txtBoxPriceRegex.Text;
            rule.ProductUrlFilter = this.txtProductUrlFilter.Text;
            rule.Charset = this.txtCharset.Text;

            if (!Rules.Instance.Contain(rule))
            {
                Rules.Instance.AddRuleToXML(rule);

                DataGridViewRow row = null;
                row = new DataGridViewRow();
                row.CreateCells(gridRules);
                row.Cells[0].Value = rule.Name;
                row.Cells[1].Value = rule.UrlPrefix;
                row.Cells[2].Value = rule.ProductUrlFilter;
                row.Cells[3].Value = rule.Charset;
                row.Cells[4].Value = rule.TitleRegex;
                row.Cells[5].Value = rule.DescriptionRegex;
                row.Cells[6].Value = rule.ImageUrlRegex;
                row.Cells[7].Value = rule.PriceRegex;
                this.gridRules.Rows.Add(row);
            }
            blank();
        }

        private void deleteRule()
        {
            DataGridViewRow row = gridRules.CurrentRow;
            string name = row.Cells[1].Value.ToString();
            Rules.Instance.DeleteRuleFromXML(name);
            blank();
            this.gridRules.Rows.Remove(row);
            blank();
        }

        private void modifyRule()
        {
            Rules rule = new Rules();
            rule.Name = this.txtBoxName.Text;
            rule.UrlPrefix = this.txtBoxURLPrefix.Text;
            rule.TitleRegex = this.txtBoxTitleRegex.Text;
            rule.DescriptionRegex = this.txtBoxDescriptionRegex.Text;
            rule.ImageUrlRegex = this.txtBoxImageURLRegex.Text;
            rule.PriceRegex = this.txtBoxPriceRegex.Text;
            rule.ProductUrlFilter = this.txtProductUrlFilter.Text;
            rule.Charset = this.txtCharset.Text;


            if (Rules.Instance.Find(rule.Name)!=null)
            {
                Rules.Instance.ModifyRuleToXML(rule);

                DataGridViewRow row = gridRules.CurrentRow;
                row.Cells[0].Value = rule.Name;
                row.Cells[1].Value = rule.UrlPrefix;
                row.Cells[2].Value = rule.ProductUrlFilter;
                row.Cells[3].Value = rule.Charset;
                row.Cells[4].Value = rule.TitleRegex;
                row.Cells[5].Value = rule.DescriptionRegex;
                row.Cells[6].Value = rule.ImageUrlRegex;
                row.Cells[7].Value = rule.PriceRegex;
                blank();
            }
            else
            {
                MessageBox.Show("The rules doesn't exists");
            }
        }
        #endregion



        private void frmRules_Load(object sender, EventArgs e)
        {
            DataGridViewRow row = null;

            foreach (Rules rule in Rules.Instance.DicRules.Values)
            {
                row = new DataGridViewRow();
                row.CreateCells(gridRules);
                row.Cells[0].Value = rule.Name;
                row.Cells[1].Value = rule.UrlPrefix;
                row.Cells[2].Value = rule.ProductUrlFilter;
                row.Cells[3].Value = rule.Charset;
                row.Cells[4].Value = rule.TitleRegex;
                row.Cells[5].Value = rule.DescriptionRegex;
                row.Cells[6].Value = rule.ImageUrlRegex;
                row.Cells[7].Value = rule.PriceRegex;
                this.gridRules.Rows.Add(row);
            }
        }

        private void gridRules_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = gridRules.CurrentRow;
            this.txtBoxName.Text = row.Cells[0].Value.ToString();
            this.txtBoxURLPrefix.Text = row.Cells[1].Value.ToString();
            this.txtProductUrlFilter.Text = row.Cells[2].Value.ToString();
            this.txtCharset.Text = row.Cells[3].Value.ToString();
            this.txtBoxTitleRegex.Text = row.Cells[4].Value.ToString();
            this.txtBoxDescriptionRegex.Text = row.Cells[5].Value.ToString();
            this.txtBoxImageURLRegex.Text = row.Cells[6].Value.ToString();
            this.txtBoxPriceRegex.Text = row.Cells[7].Value.ToString();

            //this.txtBoxName.Enabled = false;
        }

        private void btBlank_Click(object sender, EventArgs e)
        {
            blank();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            addRule();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            deleteRule();
        }

        private void btModify_Click(object sender, EventArgs e)
        {
            modifyRule();
        } 

    }
}
